package com.app.qbake.module.publish.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import com.hannesdorfmann.adapterdelegates3.AdapterDelegatesManager;
import com.wind.base.adapter.DisplayItem;
import com.app.qbake.base.bean.UploadPhoto;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wind on 2017/3/8.
 */

public class PhotoAdapter extends RecyclerView.Adapter {
    private AdapterDelegatesManager<List<DisplayItem>> delegatesManager;

    private List<DisplayItem> items;
    public PhotoAdapter(Activity activity,boolean hideDelete,int photoLayoutRes){
        items=new ArrayList<>();
        delegatesManager=new AdapterDelegatesManager<>();
        delegatesManager
                .addDelegate(new PhotoAddItemDelegate(activity))
                .addDelegate(new PhotoPathItemDelegate(activity,hideDelete,photoLayoutRes));
    }
    public PhotoAdapter(Activity activity,boolean hideDelete,int photoLayoutRes,int addLayoutRes){
        items=new ArrayList<>();
        delegatesManager=new AdapterDelegatesManager<>();
        delegatesManager
                .addDelegate(new PhotoAddItemDelegate(activity,addLayoutRes))
                .addDelegate(new PhotoPathItemDelegate(activity,hideDelete,photoLayoutRes));
    }
    public PhotoAdapter(Activity activity,boolean hideDelete){
       this(activity,hideDelete,0);
    }
    @Override public int getItemViewType(int position) {
        return delegatesManager.getItemViewType(items, position);
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return delegatesManager.onCreateViewHolder(parent,viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        delegatesManager.onBindViewHolder(items,position,holder);
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    public void add(UploadPhoto photo) {
        int size=items.size();
        items.add(photo);
        //notifyItemInserted(size);
        notifyDataSetChanged();
    }

    public void addFirst(UploadPhoto photo){
        items.add(0,photo);
    }
    public void removeLast(){
        items.remove(items.size()-1);
    }

    public void remove(int position){
        items.remove(position);
    }
    public void remove(UploadPhoto photo){
        items.remove(photo);
    }
    public void addAll(List<UploadPhoto> photoList) {
        int startPosition=items.size();
        items.addAll(photoList);

        //notifyItemRangeInserted(startPosition,photoList.size());
    }

    public List<DisplayItem> getItems(){
        return items;
    }

    public DisplayItem find(String pid) {

        for (DisplayItem item:items){
            if (item instanceof UploadPhoto){
                UploadPhoto photo= (UploadPhoto) item;
                if (pid.equals(photo.getResId()+"")){
                    return photo;
                }
            }

        }

        return null;
    }



}
